package com.lagou.server;

import com.lagou.Exception.AgeException;
import com.lagou.model.Exam;
import com.lagou.model.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 编程实现数据的存取
 */
public class ServerDao {
    private List<User> serverList = new ArrayList<>();
    private List<Exam> serverExamList = new ArrayList<>();

    /**
     * 编程实现管理员账号和密码的校验并将结果返回出去
     *
     * @param list
     * @return
     */
    public boolean serverManagerCheck(List<User> list) {
        User user = list.get(0);
        if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
            return true;
        }
        return false;
    }

    /**
     * 服务器更新密码
     * @param list
     * @return
     */
    public User serverUpdatePassword(List<User> list) {
        User user = list.get(0);
        System.out.println("修改密码接收到的：" + user.toString());
        for (int i = 0; i < serverList.size(); i++) {
            if (user.getUsername().equals(serverList.get(i).getUsername())) {
                serverList.get(i).setPassword(user.getPassword());
                System.out.println("修改密码后的学员信息为："+serverList.get(i));
                return serverList.get(i);
            }
        }
        return null;
    }

    /**
     * 学员登录
     *
     * @param list
     * @return
     */
    public User serverUserLogin(List<User> list) {
        User user = list.get(0);
        for (User user1 : serverList) {
            if (user.getUsername().equals(user1.getUsername()) && user.getPassword().equals(user1.getPassword())) {
                return user1;
            }
        }
        return null;
    }

    /**
     * 服务器新增学员
     *
     * @param list
     * @return 1代表添加成功，2代表学号已经有人使用
     */
    public boolean serverAddUser(List<User> list) {
        User user = list.get(0);
        int id = user.getId();
        //判断学号是否已经有学生使用
        for (int i = 0; i < serverList.size(); i++) {
            User student1 = serverList.get(i);
            if (student1.getId().equals(id)) {
                return false;
            }
        }
        user.setUsername(user.getName());
        user.setPassword("123456");
        user.setGrade(0);
        serverList.add(user);
        System.out.println("数据库学员信息个数：" + serverList.size());
        System.out.println("数据库新增学员信息：" + user.toString());
        return true;
    }

    /**
     * 服务器删除学员
     *
     * @param list
     * @return
     */
    public boolean serverDeleteUser(List<User> list) {
        User user = list.get(0);
        Integer id = user.getId();
        boolean isDelete = false;
        for (int i = 0; i < serverList.size(); i++) {
            User user1 = serverList.get(i);
            if (id.equals(user1.getId())) {
                serverList.remove(user1);
                System.out.println(user1.toString() + "已经删除");
                return true;
            }
        }
        if (isDelete == false) {
            System.out.println("学号为" + id + "的学生不存在");
        }
        return false;
    }

    /**
     * 服务器查找学员信息
     *
     * @param list
     * @return
     */
    public User serverSelectUser(List<User> list) {
        User user = list.get(0);
        Integer id = user.getId();
        boolean isFind = false;
        for (int i = 0; i < serverList.size(); i++) {
            User user1 = serverList.get(i);
            if (id.equals(user1.getId())) {
                System.out.println("查找成功！" + user1.toString());
                return user1;
            }
        }
        if (isFind == false) {
            System.out.println("学号为" + id + "的学生不存在");
        }
        return null;
    }

    /**
     * 服务器修改用户信息
     *
     * @param list
     * @return
     * @throws AgeException
     */
    public User serverUpdateUser(List<User> list) throws AgeException {
        User user = list.get(0);
        Integer id = user.getId();
        boolean isUpdated = false;
        for (int i = 0; i < serverList.size(); i++) {
            User user1 = serverList.get(i);
            if (id.equals(user1.getId())) {
                user1.setAge(user.getAge());
                user1.setName(user.getName());
                user1.setGrade(user.getGrade());
                System.out.println("修改后，" + user1.toString());
                return user1;
            }
        }
        if (isUpdated == false) {
            System.out.println("修改失败！");
        }
        return null;
    }

    /**
     * 服务器遍历学员信息，即得到全部学员信息
     *
     * @return
     */
    public List<User> serverGetUserList() {
        if (null != serverList) {
            return serverList;
        }
        return null;
    }

    //------------------------------------------------考题与学生业务分割处

    /**
     * 服务器添加题目信息
     *
     * @param list
     * @return
     */
    public boolean serverAddExam(List<Exam> list) {
        Exam exam = list.get(0);
        Integer id = exam.getId();
        //判断题目号是否已经使用
        for (int i = 0; i < serverExamList.size(); i++) {
            Exam exam1 = serverExamList.get(i);
            if (exam1.getId().equals(id)) {
                return false;
            }
        }
        serverExamList.add(exam);
        return true;
    }

    /**
     * 服务器删除题目信息
     *
     * @param list
     * @return
     */
    public boolean serverDeleteExam(List<Exam> list) {
        Exam exam = list.get(0);
        boolean isDelete = false;
        Integer id = exam.getId();
        for (int i = 0; i < serverExamList.size(); i++) {
            Exam exam1 = serverExamList.get(i);
            if (id.equals(exam1.getId())) {
                serverExamList.remove(exam1);
                System.out.println(exam1.toString() + "已经删除");
                return true;
            }
        }
        if (isDelete == false) {
            System.out.println("题号为" + id + "的题目不存在");
        }
        return false;
    }

    /**
     * 服务器查找题目
     *
     * @param list
     * @return
     */
    public Exam serverSelectExam(List<Exam> list) {
        Exam exam = list.get(0);
        Integer id = exam.getId();
        boolean isFind = false;
        for (int i = 0; i < serverExamList.size(); i++) {
            Exam exam1 = serverExamList.get(i);
            if (id.equals(exam1.getId())) {
                System.out.println("查找成功！" + exam1.toString());
                return exam1;
            }
        }
        if (isFind == false) {
            System.out.println("题号为" + id + "的题目不存在");
        }
        return null;
    }

    /**
     * 服务器更新考题
     * @param list
     * @return
     */
    public Exam serverUpdateExam(List<Exam> list) {
        Exam exam = list.get(0);
        Integer id = exam.getId();
        boolean isUpdated = false;
        for (int i = 0; i < serverExamList.size(); i++) {
            Exam exam1 = serverExamList.get(i);
            if (id.equals(exam1.getId())) {
                exam1.setQuestion(exam.getQuestion());
                exam1.setAnswer(exam.getAnswer());
                System.out.println("修改后，" + exam1.toString());
                return exam1;
            }
        }
        if (isUpdated == false) {
            System.out.println("修改失败！");
        }
        return null;
    }

    /**
     * 获得全部考题
     * @return
     */
    public List<Exam> serverGetExamList() {
        if (null != serverExamList) {
            System.out.println("考题有：" + serverExamList.toString());
            return serverExamList;
        }
        return null;
    }

    /**
     * 考试计算分数
     * @param list
     * @return
     */
    public int serverStartExam(List<Exam> list) {
        int grade = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAnswer().equals(serverExamList.get(i).getAnswer())) {
                grade += 10;
            }
        }
        System.out.println("分数为" + grade);
        return grade;
    }

    /**
     * 服务器导入学生信息
     * @return
     */
    public boolean inputUser() {
        ObjectInputStream ois = null;
        File file = new File("/Users/liuchangling/javaHomework/User.txt");
        try {
            //判断文件是否存在，存在则读取
            if (file.exists()) {
                ois = new ObjectInputStream(new FileInputStream(file));
                // 从输入流中读取若干个对象（通过list）并打印
                serverList = (List<User>) ois.readObject();
                System.out.println("读取学生信息文件成功!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }


    /**
     * 服务器将学生信息保存至本地
     * @param type
     * @return
     */
    public boolean outputUser(String type) {
        if (type.equals("signOut")) {
            ObjectOutputStream os = null;
            File file = new File("/Users/liuchangling/javaHomework/User.txt");
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
                os = new ObjectOutputStream(new FileOutputStream(file));
                os.writeObject(serverList);
                System.out.println("学生信息已保存至本地!");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 4.关闭流对象并释放有关的资源
                if (null != os) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return true;
        }
        return false;
    }

}
